﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.Helpers
{
    public class WebHelper
    {
        public static HttpContext GetCurrentContext()
        {
            return HttpContext.Current;
        }

        public class Pages
        {
            public static string GetPageName()
            {
                //Method 1: return System.IO.Path.GetFileName(request.PhysicalPath);
                //Method 2: return System.IO.Path.GetFileNameWithoutExtension(request.Path);
                //Method 3: return new System.IO.FileInfo(request.Url.AbsolutePath).Name; 
                var request = GetCurrentContext().Request;
                return System.IO.Path.GetFileName(request.Path);
            }     
       
            public class QueryString
            {
                public static Dictionary<string, string> GetAllParameters()
                {
                    var parameters = new Dictionary<string, string>();
                    try
                    {
                        var queryString = GetCurrentContext().Request.QueryString;
                        foreach (var key in queryString.AllKeys)
                        {
                            parameters.Add(key, queryString[key]);
                        }
                    }
                    catch (Exception)
                    {
                        //throw;
                    }
                    return parameters;
                }

                public static string GetParameter(string name)
                {
                    return GetParameter<string>(name);
                }
                public static T GetParameter<T>(string name)
                {
                    var value = default(T);
                    object valueFound = null;
                    try
                    {
                        var queryString = GetCurrentContext().Request.QueryString;
                        //value = queryString[name];
                        if ( queryString.Count > 0 )
                        {
                            var values = queryString.GetValues(name);
                            if (values != null && values.Length > 0)
                                valueFound = values.First();
                        }
                        //if ( valueFound != null )
                        //    value = (T)valueFound;
                        if ( valueFound != null )
                            value = (T)Convert.ChangeType(valueFound, typeof(T));
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    return value;
                }
            }
        }

        public class Session
        {
            public static T Get<T>(string key, HttpContext context = null)
            {
                try
                {
                    var currentContext = context ?? GetCurrentContext();
                    return (T)currentContext.Session[key];
                }
                catch (Exception)
                {
                    //throw;
                }
                return default(T);
            }
            public static void Set(string key, object value, HttpContext context = null)
            {
                var currentContext = context ?? GetCurrentContext();
                currentContext.Session[key] = value;
            }
            public static void RemoveValue(string key, HttpContext context = null)
            {
                try
                {
                    if (key.IsNotNull() && key.Length > 0)
                    {
                        var currentContext = context ?? GetCurrentContext();
                        if (currentContext.Session[key] != null)
                            currentContext.Session.Remove(key);
                    }
                }
                catch (Exception)
                {
                    //throw;
                }
            }
        }

        public class Cache
        {
            public static T GetValue<T>(string key, HttpContext context = null)
            {
                try
                {
                    var currentContext = context ?? GetCurrentContext();
                    if ( key.IsNotNull() )
                        return (T)currentContext.Cache[key];
                    else
                        return default(T);
                }
                catch (Exception)
                {
                    throw;
                }
            }

            public static void SetValue<T>(string key, T value, HttpContext context = null)
            {
                try
                {
                    if ( value.IsNull() || key.IsNull() )
                        return;

                    var currentContext = context ?? GetCurrentContext();
                    if ( GetValue<T>(key).IsNull() )
                        currentContext.Cache.Add(key, value, null, DateTime.Now.AddMonths(1), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
                    else
                        currentContext.Cache[key] = value;
                }
                catch (Exception)
                {
                    throw;
                }
            }

            public static void RemoveValue(string key, HttpContext context = null)
            {
                try
                {
                    if ( key.IsNotNull() && key.Length > 0 )
                    {
                        var currentContext = context ?? GetCurrentContext();
                        if (currentContext.Cache[key] != null)
                        currentContext.Cache.Remove(key);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }
}